#!/usr/bin/make -f

pkg_name = temurin-17-jre
priority = 1712
jvm_tools = java jfr jrunscript keytool rmiregistry
amd64_tarball_url = https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.10%2B7/OpenJDK17U-jre_x64_linux_hotspot_17.0.10_7.tar.gz
amd64_checksum = 620cc0e7338f2722f3ed076ac65c0fafb575981426bac4e1970860e5e2d048f0
arm64_tarball_url = https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.10%2B7/OpenJDK17U-jre_aarch64_linux_hotspot_17.0.10_7.tar.gz
arm64_checksum = 16080d055da0962fbd6b40f659a98a457cba3efa7ea716d5400cfebe8b935bf0
armhf_tarball_url = https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.10%2B7/OpenJDK17U-jre_arm_linux_hotspot_17.0.10_7.tar.gz
armhf_checksum = 0378bdf6769632b182b27ba4e53b17eaefefdbafa3845c15e1bd88a5aeec8442
ppc64el_tarball_url = https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.10%2B7/OpenJDK17U-jre_ppc64le_linux_hotspot_17.0.10_7.tar.gz
ppc64el_checksum = 4e18b60dba540b5c431ff03f74a1c73b22d83151f93b8768241d264d1a53582d
s390x_tarball_url = https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.10%2B7/OpenJDK17U-jre_s390x_linux_hotspot_17.0.10_7.tar.gz
s390x_checksum = c1b2fd232fc55e814479d7585d7ec45bae952a2f4137084f1d99f958c6880a49

d = debian/$(pkg_name)
jvm_home = usr/lib/jvm
jvm_dir = $(pkg_name)-$(DEB_HOST_ARCH)
pkg_alias = $(pkg_name)-$(DEB_HOST_ARCH)

%:
	dh $@

override_dh_auto_clean:
override_dh_auto_test:
override_dh_auto_build:
override_dh_strip:
override_dh_auto_install:
	# Download and verify checksum of JRE
	wget --progress=dot:mega -O jre.tar.gz $($(DEB_HOST_ARCH)_tarball_url)
	echo '$($(DEB_HOST_ARCH)_checksum) jre.tar.gz' > jre.tar.gz.sha256.txt
	sha256sum -c jre.tar.gz.sha256.txt

	# Substitute variables.
	sed \
		-e 's/@pkg_alias@/$(pkg_alias)/g' \
		-e 's/@jvm_dir@/$(jvm_dir)/g' \
		-e 's/@priority@/$(priority)/g' \
		-e 's/@jvm_tools@/$(jvm_tools)/g' \
	    debian/jinfo.in > debian/jinfo

	sed \
		-e 's/@pkg_alias@/$(pkg_alias)/g' \
		-e 's/@jvm_dir@/$(jvm_dir)/g' \
		-e 's/@priority@/$(priority)/g' \
		-e 's/@jvm_tools@/$(jvm_tools)/g' \
	    debian/postinst.in > debian/postinst

	sed \
		-e 's/@pkg_alias@/$(pkg_alias)/g' \
		-e 's/@jvm_dir@/$(jvm_dir)/g' \
		-e 's/@priority@/$(priority)/g' \
		-e 's/@jvm_tools@/$(jvm_tools)/g' \
	    debian/prerm.in > debian/prerm

	# Unpack JRE.
	dh_installdirs \
		$(jvm_home) \
		$(jvm_home)/$(jvm_dir)
	tar --strip-components=1 -C "$(d)/$(jvm_home)/$(jvm_dir)" -xf jre.tar.gz

	# Add jinfo file (alternatives support).
	cp debian/jinfo "$(d)/$(jvm_home)/.$(pkg_alias).jinfo"

	# Replace bundled cacerts and redirect to adoptium-ca-certificates.
	rm -f "$(d)/$(jvm_home)/$(jvm_dir)/lib/security/cacerts"
	ln -s /etc/ssl/certs/adoptium/cacerts "$(d)/$(jvm_home)/$(jvm_dir)/lib/security/cacerts"

override_dh_gencontrol:
	dh_gencontrol --
